using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Petra.Gis;
using Petra.Gis.Geometry;
using Petra.Gis.Geometry.Support;
using Petra.Gis.DataServices.WKT.Support;
using System.IO;

namespace Petra.Gis.DataServices.WKT
{
  public partial class GeometryFactory
  {
    /***********************************************
     * 28.8.2007
     * 
     * creates MultiPoint
     * from wkt string
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/
    /// <summary>
    /// multiPointToString takes Point only geometry collection as input
    /// and converts all elements in collection from startIndex to binary
    /// startIndex is first element to process
    /// </summary>
    //private static GeometryBase ToMultiPoint(ref string inputStr, System.Type parentGeomType)
    private static GeometryBase ToMultiPoint(RegexGeometry rg, System.Type parentGeomType)
    {

      //rgp = new RegexGeometryParser(WKTGeometryType.MultiPoint);
      //rg = rgp.ParseGeometry(ref inputStr);

      //multipoint is parsed as series of coords, need to extract coords for each point

      MultiPoint oGeom = new MultiPoint();
      Point tmpGeom;

      int numPoints = (rg.EndIndex - rg.StartIndex + 1) / (int)Dimension.dim2D;
      int j = rg.StartIndex;

      for (int i = 0; i < numPoints; i++)
      {
        tmpGeom = ToPoint(rg, j, WKTGeometryType.MultiPoint);
        oGeom.Add(tmpGeom);
        j += (int)Dimension.dim2D;
      }

      return oGeom;

      /*
      //prepare regex expr.
      string pattern = @"(?<whitespace>\s*)" +
        @"(?<geometryname>" + getWktName[WKTGeometryType.MultiPoint] + ")" +
        @"(?<openbracket>\s*\({1})" +
        @"(?<coordinate>(\s*|\s*\,{1}\s*|\s+)\-?(\d*\.?\d+|\d+\.?\d*)([eE][+-]?\d+)?)+" +
        @"(?<closebracket>\s*\){1})"; //+
      //@"(?<invalid>[^\s]+)"; //put all regex exps here to avoid invalid chars in input string

      Match m;

      Regex r = new Regex(pattern);

      m = r.Match(inputStr);
      if (m.Length < 1)
        throw new ArgumentException("invalid input string for wkt", "inputStr");


      int numCoords = m.Groups["coordinate"].Length / (int)Dimension.dim2D;
      Point pt;
      CoordinateList Coords;
      MultiPoint oGeom = new MultiPoint();

      for (int i = 0; i < numCoords; i+=2)
      {
        pt = ToPoint(m.Groups["coordinate"].Captures, i, (i + 1), oGeom.GetType());
        oGeom.Add(pt) ;
      }

      return oGeom;
     */
    }







    /***********************************************
     * 28.8.2007
     * 
     * creates wkt string 
     * from MultiPoint
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/

    /// <summary>
    /// multiPointToString takes Point only geometry collection as input
    /// and converts all elements in collection from startIndex to binary
    /// startIndex is first element to process
    /// </summary>
    private static string MultiPointToString(GeometryBase geometry)
    {
      MultiPoint _mp = (MultiPoint)geometry;

      System.IO.StringWriter mStrWriter = new System.IO.StringWriter();

      mStrWriter.Write(WKTGeometryType.MultiPoint.ToString());
      mStrWriter.Write("(");

      Point tmpPoint;
      string sep = "";

      foreach (GeometryBase g in _mp)
      {
        //cast GeometryBase to LineRing
        tmpPoint = (Point)g;
        mStrWriter.Write(sep);
        mStrWriter.Write(CoordinateListConverter.ToString(tmpPoint.coordList, Dimension.dim2D));
        sep = ",";
      }
      mStrWriter.Write(")");

      return mStrWriter.ToString();

    }

  }
}
